import 'dart:math';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_demo1/p2_change_notify.dart';
import 'package:provider_demo1/p3_list_provider.dart';
import 'package:provider_demo1/p1_normal.dart';
import 'package:provider_demo1/p4_proxy_provider.dart';
import 'package:provider_demo1/p5_change_proxy_provider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Provider Demo',
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Provider Demo')),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            RaisedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => Provider.value(
                          value: NormalModel('Hello', 'China', '你好', '中国'),
                          child: Normal())));
                },
                child: Text('Normal: \n最基础的Provider, 供子子孙孙访问始祖数据')),
            RaisedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => ChangeNotifierProvider(
                          create: (_) => CountProvider(),
                          child: ChangeNotifyWidget())));
                },
                child: Text('ChangeNotifierProvider: \n响应改变')),
            RaisedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => ChangeNotifierProvider(
                          create: (_) => PersonProvider(),
                          child: ListProviderWidget())));
                },
                child: Text('List ChangeNotifierProvider: \n响应数组的增删改')),
            RaisedButton(
                onPressed: () {
                  var colors = ['red', 'green', 'blue'];
                  var index = Random().nextInt(3);
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => MultiProvider(providers: [
                            Provider.value(value: colors[index]),
                            ProxyProvider<String, ProxyColor>(
                                update: (context, color, proxyProvider) {
                              var provider = ProxyColor();
                              provider.update(color);
                              return provider;
                            })
                          ], child: ProxyProviderWidget())));
                },
                child: Text('ProxyProvider: \n基于已有的Provider生成一个新的Provider')),
            RaisedButton(
                onPressed: () {
                  var colors = ['red', 'green', 'blue'];
                  var index = Random().nextInt(3);
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => MultiProvider(providers: [
                            Provider.value(value: colors[index]),
                            ChangeNotifierProxyProvider<String,
                                    ColorCountProvider>(
                                create: (context) => ColorCountProvider(),
                                update: (context, value, provider) {
                                  provider.color = value;
                                  return provider;
                                })
                          ], child: ChangeNotifyProxyWidget())));
                },
                child: Text(
                    'ChangeNotifierProxyProvider: 基于已有的Provider生成一个新的响应改变的Provider'))
          ],
        ),
      ),
    );
  }
}
